package com.devmindset.legoframework.client.mvc;

import com.allen_sauer.gwt.log.client.Log;
import com.devmindset.legoframework.client.mvc.metadata.EventMonitorStory;
import com.devmindset.legoframework.client.mvc.metadata.HasStory;

/**
 * Application wide base class event monitor. It's role in this application is to control
 * the {@link LegoComponent}s, using the this{@link #handleEvent(LegoEvent)} method.
 * <br/><br/>
 * 
 * This is the only place in the application where the RPC async calls can be made
 * and {@link LegoModel} should be prepared to be later passed onto the {@link LegoComponent}s
 * to just render themselves.<br/><br/>
 * 
 * Each event monitor after it's creation should be registered into it's governor the {@link EventMonitorManager}.
 * 
 * @author <a href="mailto:ice.penov@devmindset.com">Ice Penov</a>
 */
public abstract class EventMonitor implements HasStory<EventMonitorStory> {
    
  /**
   * Place to handle the events registered to this event monitor.
   * 
   * @param event the event containing it's model
   */
  public abstract void handleEvent(LegoEvent event);
  
  /**
   * This method is the only way of this component to affect the outside world,
   * by signaling an event that other {@link EventMonitor} / {@link LegoComponent} 
   * pair should handle.
   * 
   * @param event the event to be broadcasted
   */
  public void broadcastEvent(LegoEvent event) {
    Log.debug("Broadcasted event type [ " + event.getType() + " ] from source [ "
        + this.myStory() + " ]");
    EventMonitorManager.dispathEvent(event);
  }
   
}